<?php
/**
 * Dirox Framework
 * Copyright (C) 2005 - 2007 Dirox / Synexser
 * 
 * All rights reserved.
 */

require_once 'Pager.php';

class DRX_Pager
{
	var $db_object = null;
	var $pg_object = null;
    var $extraVars = array();
	var $BackLink;
	var $NextLink;
	var $PageListLink;
	var $current_page = 0;
	var $ajax;
	var $functionName;
	var $options;

	function DRX_Pager(&$db_object, $itemPerPage=PAGING_DEFAULT_ITEM_PER_PAGE, $extraVars = array())
	{
		$this->db_object 		= $db_object;
        $this->extraVars 		= $extraVars;
        $this->implementPaging($itemPerPage);

	}

	function getNavPaging()
	{
		$this->SetBackLink();
		$this->SetNextLink();
		$this->SetPageListLink();
	}

	function implementPaging($itemPerPage)
	{
		$object_page = 'page';
		$this->current_page = DRX::load("page");
		if( $this->current_page == 0 ) $this->current_page = 1;
		$numrec =  ($this->db_object->N>0?$this->db_object->N:$this->db_object->count());
		
		$this->options = array(
			'totalItems' 			=> $numrec,
			'perPage' 	 			=> $itemPerPage,
			'delta'		 			=> PAGING_DEFAULT_PAGE_DELTA,
			'httpMethod' 			=> 'GET',
			'currentPage' 			=> $this->current_page,
			'linkClass'				=> PAGING_CSS_LINK_CLASS,
			'urlVar' 				=> $object_page,
			'altPrev' 				=> PAGING_PREV,
			'altNext' 				=> PAGING_NEXT,
			'separator' 			=> PAGING_SEPARATOR,
            'extraVars' 			=> $this->extraVars,
			'curPageLinkClassName' 	=> PAGING_CSS_CURRENT_PAGE,
			'path' 					=> ""
		);

		$this->pg_object = Pager::factory($this->options);

		if($this->current_page > $this->pg_object->numPages())
			$this->current_page = $this->pg_object->numPages();

		$this->db_object->limit(($this->current_page-1)*$itemPerPage, $itemPerPage);
	}

	function SetBackLink()
	{
		$objUrl = new DRX_UrlParser();
		$root_path = $objUrl->GetRootPath();
		$link = $objUrl->GetModRewriteURL("page")."page/".($this->current_page-1);
		if($this->pg_object->_getBackLink()!="")
		{
			if($this->ajax === true) {
        		$ajax_function   = ' onclick="return '.$this->functionName.'('.($this->current_page-1).')"';
       		}
			$this->BackLink = "<a class='". PAGING_CSS_LINK_CLASS ."' href='".$root_path.$link."'".$ajax_function.">".PAGING_PREV."</a>".PAGING_SEPARATOR;
		}

	}

	function SetNextLink()
	{
		$objUrl = new DRX_UrlParser();
		$root_path = $objUrl->GetRootPath();
		$link = $objUrl->GetModRewriteURL("page")."page/".($this->current_page+1);
		if($this->pg_object->_getNextLink()!="")
		{
			if($this->ajax === true) {
        		$ajax_function   = ' onclick="return '.$this->functionName.'('. ($this->current_page+1) .')"';
       		}
			$this->NextLink = "<a class='". PAGING_CSS_LINK_CLASS ."' href='".$root_path.$link."'".$ajax_function.">".PAGING_NEXT."</a>";

		}
	}

	function SetPageListLink()
	{
		$objUrl = new DRX_UrlParser();
		$root_path = $objUrl->GetRootPath();
		$link = $objUrl->GetModRewriteURL("page");
		$total_page = $this->pg_object->numPages();
		
		if($total_page>0)
		{
			list($min, $max) = $this->pg_object->getPageRangeByPageId();

			for($i = $min; $i <= $max; $i++)
			{
				$ajax_function = "";
				if($this->ajax === true) {
        			$ajax_function   = ' onclick="return '.$this->functionName.'('.$i.')"';
       			}
				if($i==$this->current_page)
					$this->PageListLink.= "<span class='".PAGING_CSS_CURRENT_PAGE."'>".$i."</span>".PAGING_SEPARATOR;
				else
					$this->PageListLink.= "<a href='".$root_path.$link."page/{$i}' class='".PAGING_CSS_LINK_CLASS."'".$ajax_function.">".$i."</a>".PAGING_SEPARATOR;
			}
		}

	}
	
	function setFunctionForAjaxCall($functionName)
	{
		$this->ajax = true;
		$this->functionName = $functionName;
	}
	
	function disableAJAX()
	{
		$this->ajax = false;
	}

	function display()
	{
		if ($this->options['perPage'] >= $this->options['totalItems']) {
			return "";
		}
		
		$this->getNavPaging();
		echo $this->BackLink.$this->PageListLink.$this->NextLink;
	}

	function returnHTML()
	{
		if ($this->options['perPage'] >= $this->options['totalItems']) {
			return "";
		}
		
		$this->getNavPaging();
		return $this->BackLink.$this->PageListLink.$this->NextLink;
	}
	
	function returnRange()
	{
		if ($this->options['perPage'] >= $this->options['totalItems']) {
			return "";
		}
		
		$max = $this->current_page * $this->options['perPage'];
		$min = $max - $this->options['perPage'] + 1;
		$max = ($max > $this->options['totalItems']) ? $this->options['totalItems'] : $max;
		
		if ($min == $max) {
			return "$min of ".$this->options['totalItems'];
		} else {
			return "$min - $max of ".$this->options['totalItems'];
		}
	}
}